/*
 * DocReader.cpp
 *
 *  Created on: 2010-12-2
 *      Author: Administrator
 */
#include "DocReader.h"
#include <ZLInputStream.h>

DocReader::DocReader(const std::string &encoding) : EncodedTextReader(encoding) {
}

DocReader::~DocReader() {
}


void DocReader::readDocument(ZLInputStream &stream){
	if (!stream.open()) {
			return;
		}

		startDocumentHandler();

		const size_t BUFSIZE = 2048;
		char *buffer = new char[BUFSIZE];
		std::string str;
		size_t length;
		do {
			length = stream.read(buffer, BUFSIZE);
			char *start = buffer;
			const char *end = buffer + length;
			for (char *ptr = start; ptr != end; ++ptr) {
				if (*ptr == '\n' || *ptr == '\r') {
					bool skipNewLine = false;
					if (*ptr == '\r' && (ptr + 1) != end && *(ptr + 1) == '\n') {
						skipNewLine = true;
						*ptr = '\n';
					}
					if (start != ptr) {
						str.erase();
						myConverter->convert(str, start, ptr + 1);
						characterDataHandler(str);
					}
					if (skipNewLine) {
						++ptr;
					}
					start = ptr + 1;
					newLineHandler();
				} else if (isspace((unsigned char)*ptr)) {
					if (*ptr != '\t') {
						*ptr = ' ';
					}
				} else {
				}
			}
			if (start != end) {
				str.erase();
				myConverter->convert(str, start, end);
				characterDataHandler(str);
			}
		} while (length == BUFSIZE);
		delete[] buffer;

		endDocumentHandler();

		stream.close();
}
